#include <iostream>
#include <string>

#include "Calendar_t.h"
#include "Appointment_t.h"

Appointment_t* createApp();
bool userSetDay(DayTime_t& time);
bool userSetStartTime(DayTime_t& time);
//bool userSetTime(DayTime_t& time);

int main(int argc, char* argv[])
{
	//bool res = false;
	Calendar_t calendar;

	//Appointment_t* a1 = new Appointment_t();
	//DayTime_t startTime1(DayTime_t::Sunday, 12, 0), endTime1(DayTime_t::Sunday, 16, 0);
	//a1->setTime(startTime1, endTime1);
	//a1->setSubject("a1");

	//Appointment_t* a2 = new Appointment_t();
	//DayTime_t startTime2(DayTime_t::Sunday, 10, 0), endTime2(DayTime_t::Sunday, 17, 0);
	//a2->setTime(startTime2, endTime2);
	//a2->setSubject("a2");

	//Appointment_t* a3 = new Appointment_t();
	//DayTime_t startTime3(DayTime_t::Tuesday, 10, 0), endTime3(DayTime_t::Tuesday, 11, 0);
	//a3->setTime(startTime3, endTime3);
	//a3->setSubject("a3");

	//Appointment_t* a4 = new Appointment_t();
	//DayTime_t startTime4(DayTime_t::Friday, 10, 0), endTime4(DayTime_t::Friday, 18, 0);
	//a4->setTime(startTime4, endTime4);
	//a4->setSubject("a4");

	//Appointment_t* a5 = new Appointment_t();
	//DayTime_t startTime5(DayTime_t::Tuesday, 20, 0), endTime5(DayTime_t::Tuesday, 21, 0);
	//a5->setTime(startTime5, endTime5);
	//a5->setSubject("a5");

	//Appointment_t* a6 = new Appointment_t();
	//DayTime_t startTime6(DayTime_t::Monday, 5, 31), endTime6(DayTime_t::Monday, 7, 19);
	//a6->setTime(startTime6, endTime6);
	//a6->setSubject("a6");

	//res = calendar.insert(a1);
	//res = calendar.insert(a2);
	//res = calendar.insert(a3);
	//res = calendar.insert(a4);
	//res = calendar.remove(*a1,true);
	//res = calendar.insert(a2);
	//res = calendar.insert(a5);
	//res = calendar.insert(a6);

	//DayTime_t startTime7(DayTime_t::Wednesday, 10, 0);
	//const Appointment_t& temp = calendar.find(startTime6);
	//if(temp.isValid())
	//	calendar.duplicate(temp, startTime7);

	////std::cout<<calendar;

	//Calendar_t cal2 = calendar;
	//Calendar_t cal3;
	//cal3 = cal2;
	//
	////std::cout<<"now printing cal2"<<std::endl;
	////std::cout<<cal2;
	////std::cout<<"now removing a2 from cal2, should not affect original and cal3:"<<std::endl;
	//res= cal2.remove(*a2,false);
	////std::cout<<"this is original:"<<std::endl;
	////std::cout<<calendar;
	////std::cout<<"this is cal2:"<<std::endl;
	////std::cout<<cal2;
	////std::cout<<"this is cal3:"<<std::endl;
	////std::cout<<cal3;

	////calendar.removeAll(true);


	////////
	if(1)
	{
		int cmd=-1;

		while(cmd!=0)
		{
			std::cout<<"Please choose:"<<std::endl;
			std::cout<<"	(1) Insert appointment"<<std::endl;
			std::cout<<"	(2) Remove appointment"<<std::endl;
			std::cout<<"	(3) Find appointment"<<std::endl;
			std::cout<<"	(4) Duplicate appointment"<<std::endl;
			std::cout<<"	(9) Print calendar"<<std::endl;
			std::cout<<"	(0) Exit"<<std::endl;
			std::cin>>cmd;

			switch(cmd)
			{
			case 1: //create
				{
					Appointment_t* a = createApp();
					if(calendar.insert(a))
						std::cout<<std::endl<<"Appointment inserted"<<std::endl;
					else
					{
						std::cout<<std::endl<<"Appointment intersects existing appointment"<<std::endl;
						delete a;
					}
					break;
				}
			case 2: //remove
				{
					DayTime_t startTime;
					userSetDay(startTime);
					userSetStartTime(startTime);

					const Appointment_t& a = calendar.find(startTime);
					if(a.isValid())
					{
						if(!calendar.remove(a, true))
							std::cout<<std::endl<<"Failed to remove appointment"<<std::endl;
						else
							std::cout<<std::endl<<"Appointment removed"<<std::endl;
					}
					else
						std::cout<<std::endl<<"Appointment not found"<<std::endl;
					break;
				}
			case 3: //find
				{
					DayTime_t startTime;
					userSetDay(startTime);
					userSetStartTime(startTime);

					const Appointment_t& a = calendar.find(startTime);
					if(a.isValid())
					{
						std::cout<<std::endl<<"Appointment found"<<std::endl;
					}
					else
						std::cout<<std::endl<<"Appointment not found"<<std::endl;
					break;
				}
			case 4: //duplicate
				{
					DayTime_t startTime;
					userSetDay(startTime);
					userSetStartTime(startTime);

					const Appointment_t& a = calendar.find(startTime);
					if(a.isValid())
					{
						std::cout<<std::endl<<"Appointment found. Now enter day and time to copy appointment to"<<std::endl;
						
						DayTime_t newStartTime;
						userSetDay(newStartTime);
						userSetStartTime(newStartTime);

						if(calendar.duplicate(a, newStartTime))
							std::cout<<std::endl<<"Appointment duplicated successfully"<<std::endl;
						else
							std::cout<<std::endl<<"Failed to duplicate appointment"<<std::endl;
					}
					else
						std::cout<<std::endl<<"Appointment not found"<<std::endl;
					break;
				}
			case 9:
				//system("cls");
				std::cout<<calendar;
				break;
			case 0:
				calendar.removeAll(true);
				break;
			default:
				break;
			}
		}
	}
	return 0;
}

Appointment_t* createApp()
{
	Appointment_t* a = new Appointment_t;
	DayTime_t startTime, endTime;

	// ask user for the day
	userSetDay(startTime);

	// copy the day into endTime too (future prespective: support multiple day meetings)
	endTime.setDay(startTime.day());

	// ask user for start time
	userSetStartTime(startTime);

	// ask user for end time, and check validity of input
	while(1)
	{
		size_t temp;
		std::cout<<"Enter appointment end hour (0-23): ";
		std::cin>>temp;
		if(endTime.setHours(temp))
		{
			std::cout<<"Enter appointment end minute (0-59): ";
			std::cin>>temp;
			if(endTime.setMinutes(temp))
			{
				if(a->setTime(startTime, endTime))
					break;
				else
					std::cout<<"Invalid end time: should be larger than start time"<<std::endl;
			}
			else
				std::cout<<"Invalid end time: should be larger than start time"<<std::endl;

		}
		else
			std::cout<<"Invalid end hour"<<std::endl;
	}

	while(1)
	{
		std::string subject;
		std::cout<<"Enter appointment subject: ";
		std::cin.ignore(1000,'\n');
		std::getline(std::cin, subject);
		a->setSubject(subject);
		break;
	}

	return a;
}

bool userSetDay(DayTime_t& time)
{
	while(1)
	{
		int temp;
		std::cout<<"Enter appointment day (1:Sunday - 7:Saturday): ";
		std::cin>>temp;
		if(temp>=1 && temp<=7)
		{
			if(time.setDay((DayTime_t::Day_t)temp))
				break;
		}

		std::cout<<"Invalid day"<<std::endl;
	}

	return true;
}

bool userSetStartTime(DayTime_t& time)
{
	while(1)
	{
		size_t temp;
		std::cout<<"Enter appointment start hour (0-23): ";
		std::cin>>temp;
		if(time.setHours(temp))
			break;
		else
			std::cout<<"Invalid start hour"<<std::endl;
	}

	while(1)
	{
		size_t temp;
		std::cout<<"Enter appointment start minute (0-59): ";
		std::cin>>temp;
		if(time.setMinutes(temp))
			break;
		else
			std::cout<<"Invalid start minute"<<std::endl;
	}

	return true;
}

//bool userSetTime(DayTime_t& time)
//{
//	DayTime_t startTime, endTime;
//
//	// ask user for the day
//	userSetDay(startTime);
//
//	// copy the day into endTime too (future prespective: support multiple day meetings)
//	endTime.setDay(startTime.day());
//
//	// ask user for start time
//	userSetStartTime(startTime);
//
//	// ask user for end time, and check validity of input
//	while(1)
//	{
//		size_t temp;
//		std::cout<<"Enter appointment end hour (0-23): ";
//		std::cin>>temp;
//		if(endTime.setHours(temp))
//		{
//			std::cout<<"Enter appointment end minute (0-59): ";
//			std::cin>>temp;
//			if(endTime.setMinutes(temp))
//			{
//				if(a->setTime(startTime, endTime))
//					break;
//				else
//					std::cout<<"Invalid end time: should be larger than start time"<<std::endl;
//			}
//			else
//				std::cout<<"Invalid end time: should be larger than start time"<<std::endl;
//
//		}
//		else
//			std::cout<<"Invalid end hour"<<std::endl;
//	}
//
//	return true;
//}